package com.lin.controller.user.community;

import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.lin.common.exception.BaseException;
import com.lin.common.result.Result;
import com.lin.entity.DynamicData;
import com.lin.entity.vo.DynamicEntityVO;
import com.lin.service.CommunityService.CommunityService;
import com.lin.service.DynamicDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/dynamic/data")
@Api(tags = "社区动态列表点赞等相关接口")
@Slf4j
public class DynamicDataController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DynamicDataService dynamicDataService;

    @Autowired
    private CommunityService communityService;
    //动态点赞idKey常量
    private static final String PRAISE_THUMBS_KEY = "dynamic.thumbs.id.list.key";
    private static final String PRAISE_COLLECTION_KEY = "dynamic.collection.id.list.key";
    private static final String PRAISE_FORWARD_KEY = "dynamic.forward.id.list.key";
    /**
     * 点赞收藏转发
     * @param dynamicData
     * @return
     */
    @PostMapping("/sql")
    @ApiOperation("点赞收藏转发添加数据库0~2")
    public Result setDynamicData(DynamicData dynamicData){
        log.info("点赞收藏转发添加数据库 : {}",dynamicData);
        dynamicDataService.setDynamic(dynamicData);
        return Result.success();
    }

    /**
     * 点赞收藏转发
     * @param dynamicData
     * @return
     */
    @PostMapping("/redis")
    @ApiOperation("点赞收藏转发(redis)")
    public Result setDynamicThuRedis(@RequestBody DynamicData dynamicData){
        log.info("点赞收藏转发(redis) {}",dynamicData);
        if(dynamicData.getType() == 0){
            redisTemplate.opsForSet().add(PRAISE_THUMBS_KEY,String.valueOf(dynamicData.getDynamicId()));
            redisTemplate.opsForSet().add(String.valueOf(dynamicData.getDynamicId()),String.valueOf(dynamicData.getUserId()));
        }
        if(dynamicData.getType() == 1){
            redisTemplate.opsForSet().add(PRAISE_COLLECTION_KEY,String.valueOf(dynamicData.getDynamicId()));
            redisTemplate.opsForSet().add(String.valueOf(dynamicData.getDynamicId()),String.valueOf(dynamicData.getUserId()));
        }
        if(dynamicData.getType() == 2){
            redisTemplate.opsForSet().add(PRAISE_FORWARD_KEY,String.valueOf(dynamicData.getDynamicId()));
            redisTemplate.opsForSet().add(String.valueOf(dynamicData.getDynamicId()),String.valueOf(dynamicData.getUserId()));
        }
        return Result.success();
    }

    /**
     * 取消点赞收藏
     * @param dynamicData
     * @return
     */
    @DeleteMapping("/remove")
    @ApiOperation("取消点赞收藏")
    public Result remove(@RequestBody DynamicData dynamicData){
        log.info("取消点赞收藏 : {}",dynamicData);
        Set<String> dynamicIds = null;
        if(dynamicData.getType() == 0) {
            dynamicIds = redisTemplate.opsForSet().members(PRAISE_THUMBS_KEY);
        }
        else if (dynamicData.getType() == 1) {
            dynamicIds = redisTemplate.opsForSet().members(PRAISE_COLLECTION_KEY);
        }
        if (!CollectionUtils.isEmpty(dynamicIds)) {
            for (String dynamicId : dynamicIds) {
                Set<String> userIds = redisTemplate.opsForSet().members(String.valueOf(dynamicId));
                for (String userId : userIds) {
                    if (Integer.valueOf(userId) == dynamicData.getUserId()) {
                        redisTemplate.opsForSet().remove(String.valueOf(dynamicId), userId);
                    }

                }
            }
        }else if(dynamicData.getType() == 0){
            if(dynamicDataService.delete(dynamicData)){
                DynamicEntityVO dynamicEntityVO = communityService.getDynamicDetail(dynamicData.getDynamicId());
                if(dynamicEntityVO == null){
                    throw new BaseException("未找到对应的动态");
                }
                dynamicEntityVO.setDynamicThumbs(dynamicEntityVO.getDynamicThumbs() - 1);
                communityService.updateDynamic(dynamicEntityVO);
            }else {
                throw new BaseException("未找到对应的点赞用户");
            }
        } else if (dynamicData.getType() == 1) {
            if(dynamicDataService.delete(dynamicData)) {
                DynamicEntityVO dynamicEntityVO  = communityService.getDynamicDetail(dynamicData.getDynamicId());
                if (dynamicEntityVO == null) {
                    throw new BaseException("未找到对应的动态");
                }
                dynamicEntityVO.setDynamicCollection(dynamicEntityVO.getDynamicCollection() - 1);
                communityService.updateDynamic(dynamicEntityVO);
            }else {
                throw new BaseException("未找到对应的动态收藏用户");
            }
        }
        return Result.success();
    }
}


